#include <BttnWithRnd.h>



//获得权重索引
int ButtonWithRnd::IndexWithWeight(const QList<int> &WeightList)
{
    int SumWeight=0;
    for(int i=0;i<WeightList.size();i++)
    {
        SumWeight+=WeightList[i];
    }
    int WeightAdded=0;
    int rand = QRandomGenerator::global()->generate()%SumWeight;
    for(int i=0;i<WeightList.size();i++)
    {
        WeightAdded+=WeightList[i];
        if(rand<WeightAdded)
            return i;
    }
    return -1;
}
//读取随机Id
QString ButtonWithRnd::ReadStrWt(QString FormName, QString OutType,int ID)
{

    QSqlQuery Query;
    QString Select=QString("select %1 from %2 where 序号='%3'").arg(OutType).arg(FormName).arg(ID);



    Query.exec(Select);

    Query.next();

    return Query.value(0).toString();
}
//生成生成权重列表
void ButtonWithRnd::GenWeightList(QString FormName, QList<int>& WeiList)
{

    QSqlQuery Query;
    QString Select=QString("select 区间概率数, 词条个数 from %1").arg(FormName);



    Query.exec(Select);


    Query.next();
    int ItemSize=Query.value(1).toInt();
    for(int i=0;i<ItemSize;i++)
    {
        WeiList.push_back(Query.value(0).toInt());
        Query.next();
    }


}
//读取成长值
float ButtonWithRnd::ReadGrowth(QString Subtype)
{
    float tmpGrowth[4];
    QSqlQuery Query;
    QString Select=QString("select 成长值 from growth where 副词条类型 ='%1' ").arg(Subtype);


    Query.exec(Select);

    for(int i=0;i<4;i++)
    {
        Query.next();
        tmpGrowth[i]=Query.value(0).toFloat();
    }
    int rand = QRandomGenerator::global()->generate()%4;
    return tmpGrowth[rand];
}


QString ButtonWithRnd::InitMap(QString MainStr)
{
    QString Select=QString("select 对应数据表 from dataSubInitMap where 副词条类型 = '%1'").arg(MainStr);
    QSqlQuery Query;
    Query.exec(Select);
    Query.next();
    return Query.value(0).toString();
}

